import time
from threading import Thread

class Timer(Thread):
    """Class used to count time.
    When time is over, function is released"""
    
    def __init__(self, seconds,function):
        """Timer inicialization."""
        self.runTime = seconds+0.0
        self.function=function
        self.start_time=0
        Thread.__init__(self)
        self.stoped=False
    def __repr__(self):
        return "Time to End: "+str(self.timeToEnd())
    
    def timeToEnd(self):
        return round(self.runTime-(time.time()-self.start_time))
    
    def stop(self):
        self.stoped=True
    
    def run(self):
        """This function should be lauched with start"""
        self.start_time=time.time()
        time.sleep(self.runTime)
        if not self.stoped:
            self.function()
        
class Clock(Thread):
    def __init__(self, seconds,function):
        """Timer inicialization."""
        self.runTime = seconds+0.0
        self.function=function
        self.start_time=0
        Thread.__init__(self)
        self.stoped=False
    def __repr__(self):
        return "Time to End: "+str(self.timeToEnd())
    
    def timeToEnd(self):
        return round(self.runTime-(time.time()-self.start_time))
    
    def stop(self):
        self.stoped=True
    
    
    def procedure(self):
        """This function should be lauched with start"""
        self.start_time=time.time()
        time.sleep(self.runTime)
        if not self.stoped:
            self.function()
    
    def run(self):
        while(True):
            self.procedure()
    
def imprime():
    print "ola mundo - o timer terminou"

def imprime2():
    print " - terminou o 2"

if __name__ == '__main__': 
    print 
    t1 = Timer(10.0,imprime)
    t1.start()

    for i in range(10):
        print t1
        time.sleep(1)



